var ECharts;
$(document).ready(function () {

    require.config({
        baseUrl: ctx + '/resource/plugins/echarts4',
        paths: {
            echarts: 'echarts',
            roma: 'theme/roma',
        }
    });
    require(
        [
            'echarts',
            'roma'
        ],
        function (ec) {
            ECharts = ec;
            cpu_echart = ECharts.init(document.getElementById('cpu_echart'), 'roma');//端口状态可用性
            queryCpuState();

            zll_jhj = ECharts.init(document.getElementById('zll_jhj'), 'roma');//总流量
            queryKpi_zll();

            zhll_jhj = ECharts.init(document.getElementById('zhll_jhj'), 'roma');//帧流量
            queryKpi_zhll();

            dbs_jhj = ECharts.init(document.getElementById('dbs_jhj'), 'roma');//丢包数
            queryKpi_dbs();

            cbs_jhj = ECharts.init(document.getElementById('cbs_jhj'), 'roma');//错包数
            queryKpi_cbs();

            gbbs_jhj = ECharts.init(document.getElementById('gbbs_jhj'), 'roma');//广播包数
            queryKpi_gbbs();
        }
    );
});


function queryCpuState() {
    $.ajax({
        url: ctx + "/load/queryNum",
        type: "post",
        data: "EXT_UNIT_ID=" + EXT_UNIT_ID + "&KPI_CODE=" + "KPI-device-ALL-48" + "&DATE_TYPE=" + DATE_TYPE + "&CLASS_NAME=" + CLASS_NAME,
        success: function (data) {
            var mydata;
            var cpuState = JSON.parse(data);
            for (var i = 0; i < cpuState.length; i++) {
                var a = cpuState[i].SUCCESS_VALUE;
                var b = cpuState[i].COUNT_VALUE;
            }
            option = {
                tooltip: {
                    formatter: "{a} <br/>{b} : {c}%"
                },
                toolbox: {
                    show: true,
                    feature: {
                        mark: {show: true},
                        // restore: {show: true},
                        // saveAsImage: {show: true}
                    }
                },
                series: [
                    {
                        name: '',
                        type: 'gauge',
                        splitNumber: 10,       // 分割段数，默认为5
                        axisLine: {            // 坐标轴线
                            lineStyle: {       // 属性lineStyle控制线条样式
                                color: [[0.2, '#ff4500'], [0.8, '#48b'], [1, '#228b22']],//
                                width: 8
                            }
                        },
                        axisTick: {            // 坐标轴小标记
                            splitNumber: 10,   // 每份split细分多少段
                            length: 12,        // 属性length控制线长
                            lineStyle: {       // 属性lineStyle控制线条样式
                                color: 'auto'
                            }
                        },
                        axisLabel: {           // 坐标轴文本标签，详见axis.axisLabel
                            textStyle: {       // 其余属性默认使用全局文本样式，详见TEXTSTYLE
                                color: 'auto'
                            }
                        },
                        splitLine: {           // 分隔线
                            show: true,        // 默认显示，属性show控制显示与否
                            length: 30,         // 属性length控制线长
                            lineStyle: {       // 属性lineStyle（详见lineStyle）控制线条样式
                                color: 'auto'
                            }
                        },
                        pointer: {
                            width: 5
                        },
                        title: {
                            show: true,
                            offsetCenter: [0, '-40%'],       // x, y，单位px
                            textStyle: {       // 其余属性默认使用全局文本样式，详见TEXTSTYLE
                                fontWeight: 'bolder'
                            }
                        },
                        detail: {
                            formatter: '{value}%',
                            textStyle: {       // 其余属性默认使用全局文本样式，详见TEXTSTYLE
                                color: 'auto',
                                fontWeight: 'bolder'
                            }
                        },
                        data: []
                    }
                ]
            };//仪表盘结束
            var cpuName = "";
            var succ = [];
            var sun = [];
            var p_value = 0;
            var q_value = 0;
            var succs = 0;
            var sums = 0;
            if (DATE_TYPE == "showYear") {
                for (var j = 0; j < cpuState.length; j++) {
                    p_value = parseInt(cpuState[j].SUCCESS_VALUE);
                    succ.push(p_value);
                    q_value = parseInt(cpuState[j].COUNT_VALUE);
                    sun.push(q_value);
                    cpuName = cpuState[j].KPI_NAME;
                }
                for (var k = 0; k < succ.length; k++) {
                    succs += succ[k];
                    sums += sun[k];
                }
                /*alert(succs);alert(sums);*/
                mydata = [{value: parseInt(succs / sums * 100), name: cpuName}];
            } else {
                for (var i = 0; i < cpuState.length; i++) {
                    mydata = [{
                        value: parseInt(cpuState[i].SUCCESS_VALUE / cpuState[i].COUNT_VALUE * 100),
                        name: cpuState[i].KPI_NAME
                    }];
                }
            }
            if (a == undefined || b == undefined) {
                var table = "<h3 style='margin-top: 180px;padding-left: 140px;'>" + "暂无数据" + "</h3>";
                $("#cpu_echart").html(table);
            } else {
                option.series[0].data = mydata;
                cpu_echart.setOption(option);
            }
        }, error: function (textStatus) {
            console.log("error:" + textStatus.status);
        }
    });

}

function queryKpi_zll() {
    var timeHorizon = '';
    if (DATE_TYPE == 'showWeek') {
        timeHorizon = 'showOneWeek';
    } else if (DATE_TYPE == 'showMonth') {
        timeHorizon = 'showOneMonth';
    } else if (DATE_TYPE == 'showYear') {
        timeHorizon = 'showOneYear';
    } else {
        timeHorizon = 'showYesToday';
    }
    $.ajax({
        url: ctx + "/kpiContrast/queryKpiContrastInfo",
        type: "post",
        data: {extUnitId: EXT_UNIT_ID, kpiCode: 'KPI-device-ALL-27', timeHorizon: timeHorizon},
        success: function (data) {
            var list = JSON.parse(data);
            var ydata = [];
            var avgValue = [];
            var maxValue = [];
            var mixValue = [];
            var modelValue = [];
            if (list != null && list.length != 0) {
                for (var i = 0; i < list.length; i++) {
                    if (DATE_TYPE == "showWeek") {
                        x_value = list[i].DATA_TIME.split("-");
                        ydata.push(x_value[2]);
                    } else if (DATE_TYPE == "showMonth") {
                        x_value = list[i].DATA_TIME.split("-");
                        ydata.push(x_value[2]);
                    } else if (DATE_TYPE == "showYear") {
                        x_value = list[i].DATA_TIME.split("-");
                        ydata.push(x_value[1]);
                    } else {
                        x_value = list[i].DATA_TIME.replace(' ', '-').split("-");
                        ydata.push(x_value[3]);
                    }
                    avgValue.push(list[i].AVG_VALUE);
                    maxValue.push(list[i].MAX_VALUE);
                    mixValue.push(list[i].MIN_VALUE);
                    modelValue.push(list[i].MODEL_VALUE);
                }
                option = {
                    tooltip: {
                        trigger: 'axis'
                    },
                    legend: {
                        data: ['平均值', '最大值', '最小值', '众数']
                    },
                    grid: {
                        left: '3%',
                        right: '4%',
                        bottom: '3%',
                        containLabel: true
                    },
                    xAxis: {
                        type: 'category',
                        boundaryGap: false,
                        data: ydata,
                        axisLabel: {
                            interval: 0,
                            rotate: 0
                        }
                    },
                    yAxis: {
                        type: 'value'
                    },
                    series: [
                        {
                            name: '平均值',
                            type: 'line',
                            stack: '总量',
                            data: avgValue
                        },
                        {
                            name: '最大值',
                            type: 'line',
                            stack: '总量',
                            data: maxValue
                        },
                        {
                            name: '最小值',
                            type: 'line',
                            stack: '总量',
                            data: mixValue
                        },
                        {
                            name: '众数',
                            type: 'line',
                            stack: '总量',
                            data: modelValue
                        }
                    ]
                };
                zll_jhj.setOption(option);
            } else {
                $('#zll_jhj').before("抱歉，暂无数据展示。");
            }
        },
        error: function (textStatus) {
            console.log("error:" + textStatus.status);
        }
    });
}

function queryKpi_zhll() {
    var timeHorizon = '';
    if (DATE_TYPE == 'showWeek') {
        timeHorizon = 'showOneWeek';
    } else if (DATE_TYPE == 'showMonth') {
        timeHorizon = 'showOneMonth';
    } else if (DATE_TYPE == 'showYear') {
        timeHorizon = 'showOneYear';
    } else {
        timeHorizon = 'showYesToday';
    }
    $.ajax({
        url: ctx + "/kpiContrast/queryKpiContrastInfo",
        type: "post",
        data: {extUnitId: EXT_UNIT_ID, kpiCode: 'KPI-device-ALL-27', timeHorizon: timeHorizon},
        success: function (data) {
            var list = JSON.parse(data);
            var ydata = [];
            var avgValue = [];
            var maxValue = [];
            var mixValue = [];
            var modelValue = [];
            if (list != null && list.length != 0) {
                for (var i = 0; i < list.length; i++) {
                    if (DATE_TYPE == "showWeek") {
                        x_value = list[i].DATA_TIME.split("-");
                        ydata.push(x_value[2]);
                    } else if (DATE_TYPE == "showMonth") {
                        x_value = list[i].DATA_TIME.split("-");
                        ydata.push(x_value[2]);
                    } else if (DATE_TYPE == "showYear") {
                        x_value = list[i].DATA_TIME.split("-");
                        ydata.push(x_value[1]);
                    } else {
                        x_value = list[i].DATA_TIME.replace(' ', '-').split("-");
                        ydata.push(x_value[3]);
                    }
                    avgValue.push(list[i].AVG_VALUE);
                    maxValue.push(list[i].MAX_VALUE);
                    mixValue.push(list[i].MIN_VALUE);
                    modelValue.push(list[i].MODEL_VALUE);
                }
                option = {
                    tooltip: {
                        trigger: 'axis'
                    },
                    legend: {
                        data: ['平均值', '最大值', '最小值', '众数']
                    },
                    grid: {
                        left: '3%',
                        right: '4%',
                        bottom: '3%',
                        containLabel: true
                    },
                    xAxis: {
                        type: 'category',
                        boundaryGap: false,
                        data: ydata,
                        axisLabel: {
                            interval: 0,
                            rotate: 0
                        }
                    },
                    yAxis: {
                        type: 'value'
                    },
                    series: [
                        {
                            name: '平均值',
                            type: 'line',
                            stack: '总量',
                            data: avgValue
                        },
                        {
                            name: '最大值',
                            type: 'line',
                            stack: '总量',
                            data: maxValue
                        },
                        {
                            name: '最小值',
                            type: 'line',
                            stack: '总量',
                            data: mixValue
                        },
                        {
                            name: '众数',
                            type: 'line',
                            stack: '总量',
                            data: modelValue
                        }
                    ]
                };
                zhll_jhj.setOption(option);
            } else {
                $('#zhll_jhj').before("抱歉，暂无数据展示。");
            }
        },
        error: function (textStatus) {
            console.log("error:" + textStatus.status);
        }
    });
}

function queryKpi_dbs() {
    var timeHorizon = '';
    if (DATE_TYPE == 'showWeek') {
        timeHorizon = 'showOneWeek';
    } else if (DATE_TYPE == 'showMonth') {
        timeHorizon = 'showOneMonth';
    } else if (DATE_TYPE == 'showYear') {
        timeHorizon = 'showOneYear';
    } else {
        timeHorizon = 'showYesToday';
    }
    $.ajax({
        url: ctx + "/kpiContrast/queryKpiContrastInfo",
        type: "post",
        data: {extUnitId: EXT_UNIT_ID, kpiCode: 'KPI-device-ALL-27', timeHorizon: timeHorizon},
        success: function (data) {
            var list = JSON.parse(data);
            var ydata = [];
            var avgValue = [];
            var maxValue = [];
            var mixValue = [];
            var modelValue = [];
            if (list != null && list.length != 0) {
                for (var i = 0; i < list.length; i++) {
                    if (DATE_TYPE == "showWeek") {
                        x_value = list[i].DATA_TIME.split("-");
                        ydata.push(x_value[2]);
                    } else if (DATE_TYPE == "showMonth") {
                        x_value = list[i].DATA_TIME.split("-");
                        ydata.push(x_value[2]);
                    } else if (DATE_TYPE == "showYear") {
                        x_value = list[i].DATA_TIME.split("-");
                        ydata.push(x_value[1]);
                    } else {
                        x_value = list[i].DATA_TIME.replace(' ', '-').split("-");
                        ydata.push(x_value[3]);
                    }
                    avgValue.push(list[i].AVG_VALUE);
                    maxValue.push(list[i].MAX_VALUE);
                    mixValue.push(list[i].MIN_VALUE);
                    modelValue.push(list[i].MODEL_VALUE);
                }
                option = {
                    tooltip: {
                        trigger: 'axis'
                    },
                    legend: {
                        data: ['平均值', '最大值', '最小值', '众数']
                    },
                    grid: {
                        left: '3%',
                        right: '4%',
                        bottom: '3%',
                        containLabel: true
                    },
                    xAxis: {
                        type: 'category',
                        boundaryGap: false,
                        data: ydata,
                        axisLabel: {
                            interval: 0,
                            rotate: 0
                        }
                    },
                    yAxis: {
                        type: 'value'
                    },
                    series: [
                        {
                            name: '平均值',
                            type: 'line',
                            stack: '总量',
                            data: avgValue
                        },
                        {
                            name: '最大值',
                            type: 'line',
                            stack: '总量',
                            data: maxValue
                        },
                        {
                            name: '最小值',
                            type: 'line',
                            stack: '总量',
                            data: mixValue
                        },
                        {
                            name: '众数',
                            type: 'line',
                            stack: '总量',
                            data: modelValue
                        }
                    ]
                };
                dbs_jhj.setOption(option);
            } else {
                $('#dbs_jhj').before("抱歉，暂无数据展示。");
            }
        },
        error: function (textStatus) {
            console.log("error:" + textStatus.status);
        }
    });
}

function queryKpi_cbs() {
    var timeHorizon = '';
    if (DATE_TYPE == 'showWeek') {
        timeHorizon = 'showOneWeek';
    } else if (DATE_TYPE == 'showMonth') {
        timeHorizon = 'showOneMonth';
    } else if (DATE_TYPE == 'showYear') {
        timeHorizon = 'showOneYear';
    } else {
        timeHorizon = 'showYesToday';
    }
    $.ajax({
        url: ctx + "/kpiContrast/queryKpiContrastInfo",
        type: "post",
        data: {extUnitId: EXT_UNIT_ID, kpiCode: 'KPI-device-ALL-27', timeHorizon: timeHorizon},
        success: function (data) {
            var list = JSON.parse(data);
            var ydata = [];
            var avgValue = [];
            var maxValue = [];
            var mixValue = [];
            var modelValue = [];
            if (list != null && list.length != 0) {
                for (var i = 0; i < list.length; i++) {
                    if (DATE_TYPE == "showWeek") {
                        x_value = list[i].DATA_TIME.split("-");
                        ydata.push(x_value[2]);
                    } else if (DATE_TYPE == "showMonth") {
                        x_value = list[i].DATA_TIME.split("-");
                        ydata.push(x_value[2]);
                    } else if (DATE_TYPE == "showYear") {
                        x_value = list[i].DATA_TIME.split("-");
                        ydata.push(x_value[1]);
                    } else {
                        x_value = list[i].DATA_TIME.replace(' ', '-').split("-");
                        ydata.push(x_value[3]);
                    }
                    avgValue.push(list[i].AVG_VALUE);
                    maxValue.push(list[i].MAX_VALUE);
                    mixValue.push(list[i].MIN_VALUE);
                    modelValue.push(list[i].MODEL_VALUE);
                }
                option = {
                    tooltip: {
                        trigger: 'axis'
                    },
                    legend: {
                        data: ['平均值', '最大值', '最小值', '众数']
                    },
                    grid: {
                        left: '3%',
                        right: '4%',
                        bottom: '3%',
                        containLabel: true
                    },
                    xAxis: {
                        type: 'category',
                        boundaryGap: false,
                        data: ydata,
                        axisLabel: {
                            interval: 0,
                            rotate: 0
                        }
                    },
                    yAxis: {
                        type: 'value'
                    },
                    series: [
                        {
                            name: '平均值',
                            type: 'line',
                            stack: '总量',
                            data: avgValue
                        },
                        {
                            name: '最大值',
                            type: 'line',
                            stack: '总量',
                            data: maxValue
                        },
                        {
                            name: '最小值',
                            type: 'line',
                            stack: '总量',
                            data: mixValue
                        },
                        {
                            name: '众数',
                            type: 'line',
                            stack: '总量',
                            data: modelValue
                        }
                    ]
                };
                cbs_jhj.setOption(option);
            } else {
                $('#cbs_jhj').before("抱歉，暂无数据展示。");
            }
        },
        error: function (textStatus) {
            console.log("error:" + textStatus.status);
        }
    });
}

function queryKpi_gbbs() {
    var timeHorizon = '';
    if (DATE_TYPE == 'showWeek') {
        timeHorizon = 'showOneWeek';
    } else if (DATE_TYPE == 'showMonth') {
        timeHorizon = 'showOneMonth';
    } else if (DATE_TYPE == 'showYear') {
        timeHorizon = 'showOneYear';
    } else {
        timeHorizon = 'showYesToday';
    }
    $.ajax({
        url: ctx + "/kpiContrast/queryKpiContrastInfo",
        type: "post",
        data: {extUnitId: EXT_UNIT_ID, kpiCode: 'KPI-device-ALL-27', timeHorizon: timeHorizon},
        success: function (data) {
            var list = JSON.parse(data);
            var ydata = [];
            var avgValue = [];
            var maxValue = [];
            var mixValue = [];
            var modelValue = [];
            if (list != null && list.length != 0) {
                for (var i = 0; i < list.length; i++) {
                    if (DATE_TYPE == "showWeek") {
                        x_value = list[i].DATA_TIME.split("-");
                        ydata.push(x_value[2]);
                    } else if (DATE_TYPE == "showMonth") {
                        x_value = list[i].DATA_TIME.split("-");
                        ydata.push(x_value[2]);
                    } else if (DATE_TYPE == "showYear") {
                        x_value = list[i].DATA_TIME.split("-");
                        ydata.push(x_value[1]);
                    } else {
                        x_value = list[i].DATA_TIME.replace(' ', '-').split("-");
                        ydata.push(x_value[3]);
                    }
                    avgValue.push(list[i].AVG_VALUE);
                    maxValue.push(list[i].MAX_VALUE);
                    mixValue.push(list[i].MIN_VALUE);
                    modelValue.push(list[i].MODEL_VALUE);
                }
                option = {
                    tooltip: {
                        trigger: 'axis'
                    },
                    legend: {
                        data: ['平均值', '最大值', '最小值', '众数']
                    },
                    grid: {
                        left: '3%',
                        right: '4%',
                        bottom: '3%',
                        containLabel: true
                    },
                    xAxis: {
                        type: 'category',
                        boundaryGap: false,
                        data: ydata,
                        axisLabel: {
                            interval: 0,
                            rotate: 0
                        }
                    },
                    yAxis: {
                        type: 'value'
                    },
                    series: [
                        {
                            name: '平均值',
                            type: 'line',
                            stack: '总量',
                            data: avgValue
                        },
                        {
                            name: '最大值',
                            type: 'line',
                            stack: '总量',
                            data: maxValue
                        },
                        {
                            name: '最小值',
                            type: 'line',
                            stack: '总量',
                            data: mixValue
                        },
                        {
                            name: '众数',
                            type: 'line',
                            stack: '总量',
                            data: modelValue
                        }
                    ]
                };
                gbbs_jhj.setOption(option);
            } else {
                $('#gbbs_jhj').before("抱歉，暂无数据展示。");
            }
        },
        error: function (textStatus) {
            console.log("error:" + textStatus.status);
        }
    });
}